package Renderer.HTMLDrawer;
import Tree.Node;
import Renderer.*;

/**
 * This class processes all TextLayout tag Nodes.
 * @author Nathan Scully
 * @version FINAL
 */
public class TextLayout extends Drawer
{
    /**
     * The Constuctor for this class, which calls the Drawer superclass and
     * passes the node and renderer to it.
     * @param node The Node to be passed to Drawer.
     * @param renderer The Renderer to be passed to Drawer.
     */
    public TextLayout(Node node, HTMLRenderer renderer)
    {
        super(node, renderer);
    }

    /**
     * The overwritten draw method. This processes the layout of the page. In
     * most cases this creates a clone of the current TextProperty object and
     * updates it with the new layout options that were specified by the
     * tag. This new TextProperty object is then pushed onto the Stack.
     * Other operations are newLine() calls for tags such as breakline tags
     * and increasing the indentation for blockquote tags.
     */
    public void draw()
    {
        String token = node.getToken().toLowerCase();
        TextProperty textProperty = (TextProperty)(stack.peek().clone());
        if(token.equals("title"))
        {
            textProperty.setTitle(true);
        }
        else if (token.equals("br"))
        {
            newLine();
        }
        else if (token.equals("pre"))
        {
            textProperty.setPre(true);
            textProperty.setFont("Courier New");
        }
        else if (token.equals("h1"))
        {
            textProperty.setH1(true);
        }
        else if (token.equals("h2"))
        {
            textProperty.setH2(true);
        }
        else if (token.equals("h3"))
        {
            textProperty.setH3(true);
        }
        else if (token.equals("dl"))
        {
            textProperty.setDL(true);
            newLine();
            newLine();
        }
        else if (token.equals("dt"))
        {
            textProperty.setDT(true);
        }
        else if (token.equals("dd"))
        {
            textProperty.setDD(true);
            renderer.increaseIndent();
        }
        else if (token.equals("p"))
        {
            newLine();
            newLine();
        }
        else if (token.equals("blockquote"))
        {
            textProperty.setBlock(true);
            newLine();
            renderer.increaseIndent();
        }
        stack.Push(textProperty);
    }

    /**
     * The overwritten postProcess method. This will, in most cases, remove
     * the TextPropery object that was created in draw() from the Stack,
     * effectively undoing what draw() did. Many also create new lines.
     * Some will also decrease the indent such as a close blockquote tag.
     */
    public void postProcess()
    {
        String token = node.getToken().toLowerCase();
        if(token.equals("blockquote"))
        {
            renderer.descreaseIndent();
            newLine();
        }
        if(token.equals("dd"))
        {
            renderer.descreaseIndent();
            newLine();
            newLine();
        }
        if(token.equals("p"))
        {
            newLine();
            newLine();
        }
        if(token.equals("h1") || token.equals("h2") || token.equals("h3") || token.equals("dt")) newLine();
        stack.pop();
    }
}
